<script setup lang="ts">
import { useMobileCode } from '@/composables'
import { loginApi, sendCodeApi } from '@/services/user'
import { useUSerStore } from '@/stores'
import { CodeRule, MobileRule, PasswordRule } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

// 绑定name表单  不绑定没办法输入内容
const username = ref('13211112222')
// 绑定密码
const password = ref('abc12345')
// 勾选同意 不绑定没办法勾选
const agree = ref(false)
//本地存储用户的信息
const store = useUSerStore()
// 拿到路由实例
const router = useRouter()
const route = useRoute()
const onSubmit = async () => {
  if (!agree.value) {
    return showToast('请勾选协议')
  }
  // 验证通过之后进行发请求
  //使用三元表达式来进行登录验证
  const res = isPass.value
    ? await loginApi(username.value, password.value)
    : await sendCodeApi(username.value, code.value)
  // console.log('🚀 ~ onSubmit ~ res:', res)

  store.SetUser(res.data)
  console.log(route.query.returnUrl)

  showSuccessToast('登录成功')
  router.replace((route.query.returnUrl as string) || '/user')
}
// 两个切换的表单
const isPass = ref(true)
// 短信验证码绑定
const code = ref('')

const { sendLogin, Times, form } = useMobileCode(username)
// //qq跳转
// onMounted(() => {
//   // 组件渲染完毕，使用QC生成QQ登录按钮，目的得到跳转链接
//   QC.Login({
//     btnId: 'qq'
//   })
// })

//控制图标
const isShow = ref(true)
const isShowTap = () => {
  isShow.value = !isShow.value
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;">
        <!-- 把这个改成取反才可以一直切换  -->
        <span @click="isPass = !isPass">{{
          !isPass ? '密码登录' : '短信验证码登录'
        }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
      <!-- 表单 -->
    </div>
    <van-form autocomplete="off" @submit="onSubmit">
      <van-field
        ref="form"
        placeholder="请输入手机号"
        v-model="username"
        type="tel"
        :rules="MobileRule"
        >123</van-field
      >
      <van-field
        placeholder="请输入密码"
        :type="isShow ? 'password' : 'text'"
        v-model="password"
        :rules="PasswordRule"
        show-password
        v-if="isPass"
      >
        <!--  <cpIcon name="consult-alipay"></cpIcon> -->
        <template #button>
          <cpIcon
            :name="isShow ? 'login-eye-off' : 'login-eye-on'"
            @click="isShowTap"
          ></cpIcon>
        </template>
      </van-field>
      <van-field
        placeholder="短信验证码"
        v-model="code"
        v-else
        :rules="CodeRule"
      >
        <template #button>
          <van-button
            size="small"
            :class="{ active: Times > 0 }"
            type="primary"
            @click="sendLogin"
            >{{ Times ? `${Times}秒后再次发送` : '发送验证码' }}</van-button
          >
        </template>
      </van-field>
      <div class="cp-cell">
        <!-- 这个是在这个上面绑定的,所以点击什么地方都可以 -->
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>

    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <!-- <div class="icon" id="qq">
        <img src="@/assets/qq.svg" alt="" />
      </div> -->
      <a
        @click="store.setReturnUrl($route.query.returnUrl as string)"
        class="icon"
        href="https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '/src/style/login.scss';
</style>
